package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.MySbsManagerInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tool.utils.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyInfoActivity extends BaseActivity {


    @BindView(R.id.notification_padding_layout)
    LinearLayout notificationPaddingLayout;
    @BindView(R.id.titleback_image_back)
    ImageView titlebackImageBack;
    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.next_sure)
    TextView nextSure;
    @BindView(R.id.img_header_right)
    ImageView imgHeaderRight;
    @BindView(R.id.id_edit_shop_manage)
    TextView idEditShopManage;
    @BindView(R.id.tv_shopName)
    TextView tvShopName;
    @BindView(R.id.tv_hy)
    TextView tvHy;
    @BindView(R.id.tv_join_time)
    TextView tvJoinTime;
    @BindView(R.id.tv_shop_phone)
    TextView tvShopPhone;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.img_shop_icon)
    ImageView imgShopIcon;
    @BindView(R.id.tv_shop_address)
    TextView tvShopAddress;
    @BindView(R.id.tv_shop_detail)
    TextView tvShopDetail;
    @BindView(R.id.ll_set_password)
    RelativeLayout llSetPassword;
    @BindView(R.id.id_edit_shop_operate)
    TextView idEditShopOperate;
    @BindView(R.id.tv_yy)
    TextView tvYy;
    @BindView(R.id.img_yy)
    ImageView imgYy;
    @BindView(R.id.tv_principal)
    TextView tvPrincipal;
    @BindView(R.id.tv_principal_phone)
    TextView tvPrincipalPhone;
    private MySbsManagerInfo mySbsManagerInfo;

    private ImageView img_merchant_icon;

    private long groupId;
    private long userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_my_info);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
        getdata();
    }

    private void initView() {

        groupId = getIntent().getLongExtra("groupId", 0);
        if (groupId == 0) {
            ToastUtils.showShort(mContext, "大商户ID为空");
        }
        userId = getIntent().getLongExtra("userId", 0);
        if (userId == 0) {
            ToastUtils.showShort(mContext, "用户ID为空");
        }
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "商户资料管理 ");
        showView(R.id.next_sure, false);
        setTvText(R.id.next_sure, "正常");

    }


    private void getdata() {

        showProgress("加载中...");

        String url = Config.SBS_MANAGER_INFO;
        RequestParams params = new RequestParams();
        params.put("userId", userId);
        params.put("groupId", groupId);

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {

                    JSONObject result = new JSONObject(response);

                    if (!result.optString("errorCode").equals("0000")){
                        showErrorMsg(result.optString("errorMsg"));
                        return;
                    }

                    mySbsManagerInfo = new Gson().fromJson(response, new TypeToken<MySbsManagerInfo>() {
                    }.getType());

                    if (mySbsManagerInfo != null) {
                        tvShopName.setText(StringUtils.isBlank(mySbsManagerInfo.getMerchantName()) ? "" : mySbsManagerInfo.getMerchantName());
                        tvHy.setText(StringUtils.isBlank(mySbsManagerInfo.getIndustryName()) ? "" : mySbsManagerInfo.getIndustryName());
                        tvJoinTime.setText(StringUtils.isBlank(mySbsManagerInfo.getGmtCreate()) ? "" : mySbsManagerInfo.getGmtCreate());
                        tvShopPhone.setText(StringUtils.isBlank(mySbsManagerInfo.getPhone()) ? "" : mySbsManagerInfo.getPhone());
                        tvStartTime.setText(StringUtils.isBlank(mySbsManagerInfo.getWorkTime()) ? "" : mySbsManagerInfo.getWorkTime());
                        ImageLoader.getInstance().displayImage(mySbsManagerInfo.getIcon(), imgShopIcon);
                        tvShopAddress.setText(StringUtils.isBlank(mySbsManagerInfo.getAddress()) ? "" : mySbsManagerInfo.getAddress());
                        tvShopDetail.setText(StringUtils.isBlank(mySbsManagerInfo.getMemo()) ? "" : mySbsManagerInfo.getMemo());
                        tvYy.setText(StringUtils.isBlank(mySbsManagerInfo.getBusinessLicense()) ? "" : mySbsManagerInfo.getBusinessLicense());
                        ImageLoader.getInstance().displayImage(mySbsManagerInfo.getQrImg(), imgYy);
                        tvPrincipal.setText(StringUtils.isBlank(mySbsManagerInfo.getPrincipalName()) ? "" : mySbsManagerInfo.getPrincipalName());
                        tvPrincipalPhone.setText(StringUtils.isBlank(mySbsManagerInfo.getPrincipalMobile()) ? "" : mySbsManagerInfo.getPrincipalMobile());
                    }

                } catch (Exception e) {
                    e.getMessage();

                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }




    @OnClick({R.id.titleback_btn_back, R.id.id_edit_shop_manage, R.id.ll_set_password, R.id.id_edit_shop_operate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.id_edit_shop_manage:
                startActivity(new Intent(mContext, MyEditShopManageActivity.class));
                break;
            case R.id.ll_set_password:
                if (!StringUtils.isBlank(tvShopPhone.getText().toString().trim())) {
                    EventBus.getDefault().postSticky(tvShopPhone.getText().toString().trim());
                }
                startActivity(new Intent(mContext, ActivityLoginPwdSetting.class));
                break;
            case R.id.id_edit_shop_operate:
                startActivity(new Intent(mContext, MyEditShopOperateActivity.class));
                break;
        }
    }
}
